//
//  EpointMailListViewController.m
//  EpointFrame5
//
//  Created by Pill Gong on 14-2-7.
//  Copyright (c) 2014年 Epoint. All rights reserved.
//

#import "EpointMailListViewController.h"
#import "MailListTableViewCell.h"
#import "MailInfo.h"
#import "MJRefresh.h"
#import "EpointMailDetailViewController.h"
#import "Mail_GetList_Task.h"
#import "EpointConsts.h"
#import "Mail_Delete_Task.h"

#import "SGAlertMenu.h"
#import "NSString+Addtions.h"
#import "EpointDateUtils.h"
#import "UIView+Addtions.h"
#import "EpointMailSendViewController.h"

#import "EpointMailMainViewController.h"
#import "EpointMailSginInfoListViewController.h"
#import "Mail_SignLst_Task.h"
#import "UIAlertView+Blocks.h"


#define InboxTaskId 1
#define OutboxTaskId 2
#define MailDeleteTaskId 3
#define TaskMailTaskId_R 5
#define TaskMailTaskId_S 6
#define RefreshAfterTimer 0.5

#define getsignmailtaskguid 4

@interface EpointMailListViewController () <UITableViewDataSource, UITableViewDelegate, EpointMailDetailViewControllerDelegate, UISearchBarDelegate, UISearchDisplayDelegate,MailListTableViewCellDelegate> {
    NSString* mailType;// 0:收件箱 1:发件箱 2:任务邮件
    
    int pageSize, pageIndex;
    
    MJRefreshHeaderView *_header;
    MJRefreshFooterView *_footer;
    
    NSIndexPath *selectedIndexPath, *deleteIndexPath;
    UISearchBar *sb;
    UISearchDisplayController *searchController;
    
    NSString *keyWord,*selectMailGuid;
    
    PullStyle pullStyle;
    
    int newMailCount;
    bool _isMoving;
    NSIndexPath *_swipedIndexPath;
    
    NSMutableArray* qsData;
}

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *resultDataArray;

@end

@implementation EpointMailListViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)dealloc
{
    [_header free];
    [_footer free];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    if (!ISIOS7) {
        self.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
    }
    
    mailType = [initParam objectForKey:@"MailTpye"];
    keyWord = @"";
    pageSize = 20;
    newMailCount = 0;
    [self addSearchBar];
    [self addTabelView];
    [self addRightNavigationButton];
    
    if (![mailType isEqualToString:@"0"]) {
        self.title = @"发件箱";
    } else {
        [self addHeader];
        self.title = @"收件箱";
    }
    
    pullStyle = DontPull;
    [self addFooter];

    [self showMBPHUD];
    pageIndex = 1;
    [self getData];
}

-(void)viewWillAppear:(BOOL)animated{
    [self.tableView reloadData];
}

- (void)addRightNavigationButton {

    [self setRightBarButtonItemWithImage:@"icon_nav_compose" action:@selector(newMailButtonPressed)];

}

- (void)newMailButtonPressed {
    NSMutableDictionary *params = initParam;
    EpointMailSendViewController *mailSendCtrl = [[EpointMailSendViewController alloc] initWithParams:params];
    [self.navigationController pushViewController:mailSendCtrl animated:YES];
}

- (void)addSearchBar {
    sb = [[UISearchBar alloc]initWithFrame:CGRectMake(0, 0, AppWidth, 44)];
    sb.placeholder = @"关键字检索";
    sb.delegate = self;
    [self.view addSubview:sb];
    
    searchController = [[UISearchDisplayController alloc]initWithSearchBar:sb contentsController:self];
    searchController.searchResultsTableView.backgroundColor = [UIColor clearColor];
    searchController.searchResultsTableView.separatorColor = [UIColor clearColor];
    searchController.delegate = self;
    searchController.searchResultsDataSource = self;
    searchController.searchResultsDelegate = self;
    searchController.searchResultsTableView.separatorStyle = UITableViewCellSeparatorStyleSingleLineEtched;
    searchController.searchResultsTableView.bounces = NO;
    searchController.searchResultsTableView.tag = 2;
    
}

- (void)addTabelView {
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 44, AppWidth, AppHeight-SystemTakeHeight-44)];
    _tableView.tag = 1;
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
    
    self.dataArray = [[NSMutableArray alloc] initWithCapacity:20];
}

- (void)addFooter
{
    MJRefreshFooterView *footer = [MJRefreshFooterView footer];
    footer.scrollView = self.tableView;
    footer.beginRefreshingBlock = ^(MJRefreshBaseView *refreshView) {
        // TODO: add foot data
        pullStyle = PullDownRefresh;
        if (newMailCount != 0) {
            pageIndex = _dataArray.count / pageSize;
            pageSize = 20 - newMailCount;
        } else {
            pageSize = 20;
        }
        pageIndex++;
        [self getData];
    };
    footer.endStateChangeBlock = ^(MJRefreshBaseView *refreshView) {
        // 刷新完毕就会回调这个Block
        UIEdgeInsets inset = self.tableView.contentInset;
        inset.bottom = 0;
        self.tableView.contentInset = inset;
    };
    _footer = footer;
//    [_footer beginRefreshing];
}

- (void)addHeader
{
    MJRefreshHeaderView *header = [MJRefreshHeaderView header];
    header.scrollView = self.tableView;
    header.beginRefreshingBlock = ^(MJRefreshBaseView *refreshView) {
        pullStyle = PullUpRefresh;
        pageIndex = 1;
        [self getData];
    };
    header.endStateChangeBlock = ^(MJRefreshBaseView *refreshView) {
        // 刷新完毕就会回调这个Block
    };
    header.refreshStateChangeBlock = ^(MJRefreshBaseView *refreshView, MJRefreshState state) {
        // 控件的刷新状态切换了就会调用这个block
        switch (state) {
            case MJRefreshStateNormal:
                NSLog(@"%@----切换到：普通状态", refreshView.class);
                break;
                
            case MJRefreshStatePulling:
                NSLog(@"%@----切换到：松开即可刷新的状态", refreshView.class);
                break;
                
            case MJRefreshStateRefreshing:
                NSLog(@"%@----切换到：正在刷新状态", refreshView.class);
                break;
            default:
                break;
        }
    };
//    [_header beginRefreshing];
    _header = header;
}

- (void)doneWithView:(MJRefreshBaseView *)refreshView
{
    [self.tableView reloadData];
    if (refreshView) {
        [refreshView endRefreshing];
    }
}

-(void)homeToDetail{
    if (XAppDelegate.clickItemPos!=-1) {
        MailInfo *selectedMail = [_dataArray objectAtIndex:XAppDelegate.clickItemPos];
        int selectedRow = [_dataArray indexOfObject:selectedMail];
        selectedIndexPath = [NSIndexPath indexPathForRow:selectedRow inSection:selectedIndexPath.section];
        NSMutableDictionary *params = [[NSMutableDictionary alloc]init];
        [params setObject:selectedMail forKey:@"MailInfo"];
        [params setObject:[NSString stringWithFormat:@"%d", XAppDelegate.clickItemPos] forKey:@"selectedRow"];
        [params setObject:mailType forKey:@"mailType"];
        // TODO: add some properties
        EpointMailDetailViewController *mailDetailCtrl = [[EpointMailDetailViewController alloc] initWithParams:params];
        mailDetailCtrl.delegate = self;
        mailDetailCtrl.parentCtrl = self;
        XAppDelegate.clickItemPos=-1;
        [_parentCtrl.navigationController pushViewController:mailDetailCtrl animated:YES];
    }
    
}

- (void)pullUpRefreshAction {
    [self toastMessage:@"无新邮件！"];
    [_header endRefreshing];
}

- (void)pullDownRefreshAction {
    [_footer endRefreshing];
    [self toastMessage:@"没有更多邮件！"];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)getData {
    
    NSMutableDictionary *params = [[NSMutableDictionary alloc]init];
    [params setObject:[NSString stringWithFormat:@"%d", pageSize] forKey:@"pagesize"];
    [params setObject:keyWord forKey:@"keyword"];
    [params setObject:[NSString stringWithFormat:@"%d", pageIndex] forKey:@"CurrentPageIndex"];
    
    if ([mailType isEqualToString:@"1"]) {
        // TODO: get outbox
        [params setObject:OutboxType forKey:@"boxType"];
        [self startTask:@"Mail_GetList_Task" withParams:params withTaskId:OutboxTaskId];
    } else if ([mailType isEqualToString:@"0"]){
        // TODO: get inbox
        [params setObject:InboxType forKey:@"boxType"];
        [self startTask:@"Mail_GetList_Task" withParams:params withTaskId:InboxTaskId];
    }else if ([mailType isEqualToString:@"2"]){
        // TODO: get inbox
        [params setObject:TaskMail_Type forKey:@"boxType"];
        [self startTask:@"Mail_GetList_Task" withParams:params withTaskId:TaskMailTaskId_R];
    }
//    [self addSomeDataForTest];
//    [_tableVdataArrayiew reloadData];
}

#pragma mark - UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (tableView == _tableView) {
        return _dataArray.count;
    } else {
        return _resultDataArray.count;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"CellIdentifier";
    MailListTableViewCell *cell = (MailListTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[MailListTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
//    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    MailInfo *mailInfo;
    if (tableView == _tableView) {
        mailInfo = [_dataArray objectAtIndex:indexPath.row];
    } else {
        mailInfo = [_resultDataArray objectAtIndex:indexPath.row];
    }

    if ([mailType isEqualToString:@"1"]) {
        NSString *toUserString = mailInfo.ToUserDispNameList;
        if ([toUserString hasSuffix:@";"]) {
            toUserString = [toUserString substringToIndex:toUserString.length-1];
        }
        cell.fromPerson.text =toUserString;
        cell.delegate=self;
    } else {
        cell.fromPerson.text = mailInfo.FROMDISPNAME;
    }
    cell.fromDate.text = [EpointDateUtils convertToMailListNeededFormat:mailInfo.SENDTIME];//mailInfo.SENDTIME;
    [cell.fromDate setTextColor:[UIColor grayColor]];
    cell.title.text = mailInfo.Subject;
    

    cell.hasAttach.hidden = ![mailInfo.HasAttach boolValue];

    if ([mailType isEqualToString:@"1"]) {
        cell.isReadImage.hidden = [mailInfo.IsAllSign boolValue];
    } else {
        cell.isReadImage.hidden = [mailInfo.isRead boolValue];
    }
    return cell;
}


- (void)tableView:(UITableView *)tableView
commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath {
    deleteIndexPath =indexPath;
    if ([mailType isEqualToString:@"0"]) {
        //TODO: DELETE
        [self delMailAction];
    }
}

-(void)delMailAction{
    
    RIButtonItem* cancelBtn = [RIButtonItem item];
    cancelBtn.label=@"取消";
    cancelBtn.action=^{};
    RIButtonItem* okBtn =[RIButtonItem item];
    okBtn.label=@"确定";
    okBtn.action=^{
        [self showMBPHUD];
        MailInfo *mailInfo = [_dataArray objectAtIndex:deleteIndexPath.row];
        NSMutableDictionary *params = [[NSMutableDictionary alloc]init];
        [params setObject:mailInfo.MAILGUID forKey:@"MailGuidList"];
        [self startTask:@"Mail_Delete_Task" withParams:params withTaskId:MailDeleteTaskId];

    };
    UIAlertView* delAlert =[[UIAlertView alloc] initWithTitle:@"提示" message:AreYouSureToDeleteThisMaill cancelButtonItem:cancelBtn otherButtonItems:okBtn, nil];
    [delAlert show];
}

- (void)deleteMail:(NSIndexPath *)indexPath {
    [_dataArray removeObjectAtIndex:indexPath.row];
    [_tableView deleteRowsAtIndexPaths:[NSMutableArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}

#pragma mark - UITableViewDelegate

- (NSString *)tableView:(UITableView *)tableView
titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString* operatorStr=@"";
    if ([mailType isEqualToString:@"0"]) {
        operatorStr = @"删除";
    } else {
        operatorStr = @"操作";
    }
    return operatorStr;
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    if ([mailType isEqualToString:@"0"]) {
       return UITableViewCellEditingStyleDelete;
    } else {
       return UITableViewCellEditingStyleNone;
    } 
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 75;//53;//66
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    selectedIndexPath = indexPath;

    NSMutableDictionary *params = [[NSMutableDictionary alloc]init];
    if (tableView == _tableView) {
        [params setObject:[_dataArray objectAtIndex:indexPath.row] forKey:@"MailInfo"];
    } else {
        MailInfo *selectedMail = [_resultDataArray objectAtIndex:indexPath.row];
        int selectedRow = [_dataArray indexOfObject:selectedMail];
        selectedIndexPath = [NSIndexPath indexPathForRow:selectedRow inSection:selectedIndexPath.section];
        [params setObject:selectedMail forKey:@"MailInfo"];
    }
    [params setObject:[NSString stringWithFormat:@"%d", indexPath.row] forKey:@"selectedRow"];
    [params setObject:mailType forKey:@"mailType"];
    // TODO: add some properties
    EpointMailDetailViewController *mailDetailCtrl = [[EpointMailDetailViewController alloc] initWithParams:params];
    mailDetailCtrl.delegate = self;
    mailDetailCtrl.parentCtrl = self;
    [_parentCtrl.navigationController pushViewController:mailDetailCtrl animated:YES];
    if (tableView == searchController.searchResultsTableView) {
        [self resignSearchAction];
    }
}


#pragma mark - EpointMailDetailViewControllerDelegate

- (void)mailDetailCtrlIsLoadFinished:(EpointMailDetailViewController *)detailCtrl {
    MailInfo *mailInfo;
//    if (_tableView.hidden) {
//        mailInfo = [_resultDataArray objectAtIndex:selectedIndexPath.row];
//    } else {
        mailInfo = [_dataArray objectAtIndex:selectedIndexPath.row];
//    }
    
    mailInfo.isRead = @"1";
    [_tableView reloadData];
}

- (void)changeSelectedRow:(int)selectedRow {
    selectedIndexPath = [NSIndexPath indexPathForRow:selectedRow inSection:selectedIndexPath.section];
}

- (void)mailDetailDeleteCurrentOne {
    [_dataArray removeObjectAtIndex:selectedIndexPath.row];
    [_tableView deleteRowsAtIndexPaths:[NSMutableArray arrayWithObject:selectedIndexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}

#pragma mark - UISearchDisplayDelegate

-(void)searchDisplayController:(UISearchDisplayController *)controller willHideSearchResultsTableView:(UITableView *)tableView
{
    [_tableView setHidden:NO];
}

-(void)searchDisplayController:(UISearchDisplayController *)controller willShowSearchResultsTableView:(UITableView *)tableView
{
    [_tableView setHidden:YES];
}

#pragma mark - UISearchBarDelegate

-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
    //[_dataArray removeAllObjects];
    pageIndex = 1;
    keyWord = searchText;
    if (self.resultDataArray==nil) {
        self.resultDataArray = [NSMutableArray arrayWithCapacity:20];
    }
    [_resultDataArray removeAllObjects];

//    [self findTheMailContain:searchText];
//    [searchController.searchResultsTableView reloadData];
//    [self showMBPHUD];
//    [self getData];
}

- (void)findTheMailContain:(NSString *)searchText {

    for (MailInfo *mail in _dataArray) {
        if ([mail.Subject rangeOfString:searchText].length > 0 ||
            [mail.FROMDISPNAME rangeOfString:searchText].length > 0) {
            [_resultDataArray addObject:mail];
        }
    }
}

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
    NSLog(@"searchBarSearchButtonClicked");
    [_dataArray removeAllObjects];
    pullStyle = DontPull;
    [self getData];
    [self resignSearchAction];
}

- (void)resignSearchAction {
    [sb resignFirstResponder];
    [searchController setActive:NO animated:YES];
}

#pragma mark - 

-(void)refreshUIWithThreadGuid:(int)taskId andData:(NSObject *)obj {

    [self hideMBPHUD];
    
    if(getsignmailtaskguid==taskId){
        NSString *bs = (NSString *)obj;
        qsData = [Mail_SignLst_Task getList:bs];
        NSMutableDictionary* params =[[NSMutableDictionary alloc] init];
        [params setObject:qsData forKey:@"dataArray"];
        [params setObject:selectMailGuid forKey:@"MailGuid"];
        EpointMailSginInfoListViewController *mailSignView = [[EpointMailSginInfoListViewController alloc]initWithParams:params];
        [_parentCtrl.navigationController pushViewController:mailSignView animated:YES];
        return;
    }
    
    NSMutableArray *arr = (NSMutableArray*)obj;
    if (![[arr objectAtIndex:0]isEqualToString:@"1"]) {
        [self toastMessage:[arr objectAtIndex:1]];
        switch (pullStyle) {
            case PullUpRefresh: {
                [_header endRefreshing];
            }
                break;
            case PullDownRefresh: {
                [_footer endRefreshing];
            }
                break;
            default:
                break;
        }
        return;
    }

    if (taskId == MailDeleteTaskId) {
        [self toastMessage:[arr lastObject]];
        [self deleteMail:deleteIndexPath];
    }else {

        NSArray *tempArray = [arr lastObject];
        
        MJRefreshBaseView *refreshView;
        switch (pullStyle) {
            case DontPull: {
                if (tempArray.count == 0) {
                    if ([mailType isEqualToString:@"2"]) {
                        [self toastMessage:[NSString stringWithFormat:@"暂无任务邮件！"]];
                    }else{
                        [self toastMessage:[NSString stringWithFormat:@"%@件箱中无邮件！",mailType ? @"发" : @"收"]];
                    }
                }
                [_dataArray addObjectsFromArray:tempArray];
                refreshView = nil;
                [self homeToDetail];
            }
                break;
            case PullUpRefresh: {
                [_dataArray removeAllObjects];
                [_dataArray addObjectsFromArray:tempArray];
                refreshView = _header;
            }
                break;
            case PullDownRefresh: {
                if (tempArray.count == 0) {
                    [self performSelector:@selector(pullDownRefreshAction) withObject:nil afterDelay:RefreshAfterTimer];
                } else {
                    [_dataArray addObjectsFromArray:tempArray];
                }
                refreshView = _footer;
            }
                break;
            default:
                refreshView = nil;
                break;
        }
        [self performSelector:@selector(doneWithView:) withObject:refreshView afterDelay:RefreshAfterTimer];
    }
}

- (NSArray *)checkNewMail:(NSArray *)tempArray {
    
    NSMutableArray *newMailArr = [NSMutableArray arrayWithCapacity:tempArray.count];
    
//    for (MailInfo *tempMail in tempArray) {
//        if (![_dataArray containsObject:tempMail]) {
//            [newMailArr addObject:tempMail];
//        }
//    }
    
    int searchSize = _dataArray.count>=20 ? 20 : _dataArray.count;
    
    for (int i=0; i<tempArray.count; i++) {
        MailInfo *tempMail = [tempArray objectAtIndex:i];
        BOOL isFound = NO;
        for (int j=0; j<searchSize; j++) {
            MailInfo *existMail = [_dataArray objectAtIndex:j];
            if ([tempMail.MAILGUID isEqualToString:existMail.MAILGUID]) {
                isFound = YES;
                continue;
            }
        }
        if (isFound == NO) {
            [newMailArr addObject:tempMail];
        }
    }
    
    return newMailArr;
}

#pragma mark - Test Data

- (void)addSomeDataForTest {
    NSArray *titleArr = @[@"请检查你的邮箱是否能接口邮件", @"这是一封测试邮件", @"南大街死啊老娘博大精深烂大街扩散几点开始来拿", @"这个邮件的标题也很长这个邮件的标题也很长这个邮件的标题也很长", @"这是一个标题"];
    
    NSArray *fromPersonArr = @[@"管理员", @"测试人员", @"工程师", @"一个名字很长很长很长很长很长的用户", @"邮箱管理员"];
    
    NSArray *desArray = @[@"管理员发的邮件来测试我们的邮件收发功能发的邮件来测试我们的邮件收发功能", @"测试人员发的邮件来测试我们的邮件收发功能发的邮件来测试我们的邮件收发功能", @"", @"一个名字很长很长很长很长很长的用户发的邮件来测试我们的邮件收发功能", @"邮箱管理员发的邮件来测试我们的邮件收发功能发的邮件来测试我们的邮件收发功能"];
    
    NSArray *isReadArr = @[@"0", @"0", @"1", @"1", @"0"];
    
    for (int i=0; i<20; i++) {
        int count = i%5;
        MailInfo *mailInfo = [[MailInfo alloc] init];
        mailInfo.FROMDISPNAME = [fromPersonArr objectAtIndex:count];
        mailInfo.Subject = [titleArr objectAtIndex:count];
        NSString *desString = [desArray objectAtIndex:count];
        if (desString && ![desString isEqualToString:@""]) {
            mailInfo.MailContent = desString;
        } else {
            mailInfo.MailContent = @"(无摘要)";
        }
        mailInfo.isRead = [isReadArr objectAtIndex:count];
        
        int date = i+1;
        NSString *dateString = [NSString stringWithFormat:@"%@", date>9?[NSString stringWithFormat:@"%d", date]:[NSString stringWithFormat:@"0%d", date]];
        mailInfo.SENDTIME = [NSString stringWithFormat:@"2014/02/%@", dateString];
        [_dataArray addObject:mailInfo];
    }
}

- (void)addHeaderData {
    MailInfo *mailInfo = [[MailInfo alloc] init];
    mailInfo.FROMDISPNAME = @"HeaderData";
    mailInfo.Subject = @"HeaderData";
    mailInfo.MailContent = @"(无摘要)";
    mailInfo.isRead = @"0";
    mailInfo.SENDTIME = @"2014/02/00";
    [_dataArray insertObject:mailInfo atIndex:0];
}

- (void)addFootData {
    MailInfo *mailInfo = [[MailInfo alloc] init];
    mailInfo.FROMDISPNAME = @"FootData";
    mailInfo.Subject = @"FootData";
    mailInfo.MailContent = @"(无摘要)";
    mailInfo.isRead = @"0";
    mailInfo.SENDTIME = @"2014/02/30";
    [_dataArray addObject:mailInfo];
}


#pragma mark - SwipeEnableTableViewCellDelegate

- (void)cellMoveLeftStarted:(MailListTableViewCell *)cell {
    NSLog(@"start move left ...");
    if ([self resetSwipedCell] == NO) {
        if (_isMoving == NO) {
            [cell moveLeft];
            _isMoving = YES;
        }
    }
}

- (void)cellMoveLeftEnded:(MailListTableViewCell *)cell {
    _swipedIndexPath = [_tableView indexPathForCell:cell];
    _isMoving = NO;
}

- (void)cellMoveRightStarted:(MailListTableViewCell *)cell {
    NSLog(@"start move right ...");
    if ([self resetSwipedCell] == NO) {
        [cell moveRight];
        _isMoving = YES;
    }
}

- (void)cellMoveRightEnded:(MailListTableViewCell *)cell {
    _swipedIndexPath = nil;
    _isMoving = NO;
}

- (BOOL)resetSwipedCell {
    if (_swipedIndexPath == nil) {
        return NO;
    }
    MailListTableViewCell *cell = (MailListTableViewCell *)[_tableView cellForRowAtIndexPath:_swipedIndexPath];
    [cell moveRight];
    _isMoving = YES;
    return YES;
}


- (void)cellSignButtonPressed:(MailListTableViewCell *)cell {
    if (_isMoving) {
        return;
    }
    NSIndexPath *indexPath = [_tableView indexPathForCell:cell];
    int index = indexPath.row;
    NSLog(@"index=%i",index);
    MailInfo* m = [_dataArray objectAtIndex:index];
    NSLog(@"titile=%@",m.Subject);
    selectMailGuid = m.MAILGUID;
    NSMutableDictionary *params = [[NSMutableDictionary alloc]init];
    [params setObject:selectMailGuid forKey:@"MailGuid"];
    [self startTask:@"Mail_SignLst_Task" withParams:params withTaskId:getsignmailtaskguid];
    [self showMBPHUD];
}



- (void)cellDeleteButtonPressed:(MailListTableViewCell *)cell {
    if (_isMoving) {
        return;
    }
    deleteIndexPath = [_tableView indexPathForCell:cell];
    [self delMailAction];
}

@end
